cc.Class({
    extends: cc.Component,

    properties: {
       btnAgain:cc.Button,
       btnNext:cc.Button,
       btnReturnHall:cc.Button,
       btnQiuShop:cc.Button,
       btnGanShop:cc.Button,
       txtIsPassLevel:cc.Label,
       pfShop:cc.Prefab,
       pass:cc.Node,
       notPass:cc.Node,
       txtGold:cc.Label,
       txtGoldNum:cc.Label,
       pfGold:cc.Prefab,
       startGold:cc.Node,
       endGold:cc.Node,
       goldArr:[],
    },

    onLoad () {
        this.txtGold.string = window.gameConfig.gold;
        cc.director.preloadScene("gameHall");
        cc.director.preloadScene("gameMain");

        if(window.gameEndData.endType == 1)
        {
            this.pass.active = true;
            this.notPass.active = false;
            this.setAllGoldNumber();
            if(window.gameConfig.level > window.gameEndData.gameLevel)
            {
                this.txtGoldNum.string = "+10";
            }
            else
            {
                this.txtGoldNum.string = "+50";
            }
        }
        else if(window.gameEndData.endType == 0)
        {
            this.pass.active = false;
            this.notPass.active = true;
        }

        if(window.gameEndData.gameLevel >= 30)
        {
            this.txtIsPassLevel.string = "恭喜全部通关!!!!!!";
            this.btnNext.node.active = false;
        }
        else
        {
            this.txtIsPassLevel.string = "第" + (window.gameEndData.gameLevel) + "关";
            this.btnNext.node.active = true;
        }
        
    },

    setAllGoldNumber:function(){
        var self = this;
        // this.createGoldAnimNode();
        this.scheduleOnce(()=>{
            let num;
            if(window.gameConfig.level < window.gameEndData.gameLevel)
            {
                num = parseInt(window.gameConfig.gold) + 10;
            }
            else
            {
                num = parseInt(window.gameConfig.gold) + 50;
            }
            let gameMaxLevel = window.gameConfig.level;
            if(gameMaxLevel <= window.gameEndData.gameLevel)window.gameConfig.saveLevel(parseInt(window.gameEndData.gameLevel)+1);
            self.txtGold.string = num;
            window.gameConfig.setGoldNumber(num);
            self.createGoldAnimNode();
            self.setGoldAnimation();
        },1);
    },

    createGoldAnimNode:function(){
        for(let i = 0; i < 5; i++)
        {
            let nodeGold = window.creatrPrefab(this.pfGold);
            this.endGold.addChild(nodeGold);
            let posi = this.startGold.getPosition()
            nodeGold.setPosition(posi.x, posi.y);
            this.goldArr.push(nodeGold);
        }
    },

    setGoldAnimation:function(){
        for(let i = 0; i < this.goldArr.length; i++)
        {
            this.udud(this, this.goldArr[i], i)
        }
    },

    udud:function(self, nodeGold, index){
        let posi = this.endGold.getPosition()
        var action = cc.moveTo(0.25, posi.x, posi.y);
        this.scheduleOnce(()=>{
            nodeGold.runAction(action);
        }, index * 0.1);
    },


    //返回大厅
    onBtnReturnHallCallBack:function(){
        window.audioMgr.prototype.playButtonAudio();
        cc.director.loadScene("gameHall");
    },

    //重玩
    onBtnAgainCallBack:function(){
        window.audioMgr.prototype.playButtonAudio();
        let data = window.getLevelData();
        window.setLevelData(data)
        cc.director.loadScene("gameMain");
    },
    
    //进入下一关
    onBtnNextCallBack:function(){
        window.audioMgr.prototype.playButtonAudio();
        //获取下一关的数据 并保存
        window.getGameSubLevelData(window.gameEndData.gameLevel);
        cc.director.loadScene("gameMain");
    },

    //皮肤界面
    onBtnShopCallback:function(custom){
        window.audioMgr.prototype.playButtonAudio();
        //加载皮肤选择界面prefab并根据参数加载篮球或弹杆的皮肤图片
        //加载设置界面prefab
        let pfShop = creatrPrefab(this.pfShop);
        let shop = pfShop.getComponent("gameShop");
        if(custom == 1)
        {
            shop.setShopType("ball");
        }
        else
        {
            shop.setShopType("gan");
        }

        this.node.addChild(pfShop);
    },
});
